package com.tony.server.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.tony.easymvc.mapper.DbInfoMapper;
import com.tony.easymvc.mapper.HtmlOperationMapper;
import com.tony.easymvc.mapper.ProjectInfoMapper;
import com.tony.easymvc.mapper.ProjectTablesMapper;
import com.tony.easymvc.po.DbInfo;
import com.tony.easymvc.po.HtmlOperation;
import com.tony.easymvc.po.HtmlOperationExample;
import com.tony.easymvc.po.ProjectTables;
import com.tony.po.ParaHtmlOperationBean;
import com.tony.server.service.HtmlProjectService;
import com.tony.vo.Result;

@Controller
@RequestMapping("html")
public class HtmlProjectInfoController {

	@Resource
	HtmlOperationMapper htmlOperationMapper;
	@Resource
	ProjectInfoMapper projectInfoMapper;
	@Resource
	DbInfoMapper dbInfoMapper;

	@Resource
	ProjectTablesMapper projectTablesMapper;

	@Resource
	HtmlProjectService htmlProjectService;

	@ResponseBody
	@RequestMapping("add")
	public String add(Integer bizId, String tableName, @RequestParam("data") String oper) {
		List<HtmlOperation> itemReq = JSONArray.parseArray(oper, HtmlOperation.class);
		for (HtmlOperation htmlOperation : itemReq) {
			if (htmlOperation.getIsPrimary().equals("true")&&htmlOperation.getUseType().contains("importupdate")) {			
					return "当字段为主键时，不能勾选使用类型中导入更新";	
				
				
			}
			
		}

		/*
		 * HtmlOperationExample example = new HtmlOperationExample();
		 * example.or().andProjectIdEqualTo(id).andTableNameEqualTo(tableName);
		 * List<HtmlOperation> items = htmlOperationMapper.selectByExample(example); if
		 * (items != null && !items.isEmpty()) { // 存在 for (int i = 0; i <
		 * itemReq.size(); i++) { HtmlOperation html = itemReq.get(i);
		 * 
		 * HtmlOperationExample example1 = new HtmlOperationExample();
		 * example1.or().andProjectIdEqualTo(id).andTableNameEqualTo(tableName)
		 * .andColumnNameEqualTo(html.getColumnName());
		 * htmlOperationMapper.updateByExampleSelective(html, example1); } } else { for
		 * (int i = 0; i < itemReq.size(); i++) { HtmlOperation html = itemReq.get(i);
		 * html.setProjectId(id); html.setTableName(tableName);
		 * htmlOperationMapper.insertSelective(html); } }
		 * if(StringUtils.isNotEmpty(tableName)){ ProjectTables record=new
		 * ProjectTables(); record.setUpdateTime(new Date());
		 * record.setTableName(tableName);
		 * projectTablesMapper.updateByTablesNameSelective(record); }
		 * System.out.println(tableName);
		 */
		htmlProjectService.addHtmlOperationBean(itemReq, tableName, bizId);
		return "操作成功";
	}

	@ResponseBody
	@RequestMapping("list")
	public Result list(@RequestParam("id") Integer id, @RequestParam("tableName") String tableName)
			throws SQLException {
		List<HtmlOperation> return_list = htmlProjectService.getHtmlOperation(id, tableName);
		return new Result(true, "查询成功", return_list);
	}
	
	

	
   //查询关联字段
	@ResponseBody
	@RequestMapping("associationField")
	public Result associationField(@RequestParam("id") Integer id, @RequestParam("tableName") String tableName)throws SQLException {	
		List<HtmlOperation> lIbraryHtmlOperation=htmlProjectService.htmlopera(id, tableName);
		return new Result(true, "查询成功", lIbraryHtmlOperation);
	}
	
	 //查询库
	@ResponseBody
	@RequestMapping("selectionLibrary")
	public Result selectionLibrary(@RequestParam("projectId") Integer projectId)throws SQLException {
		List<DbInfo> lIbraryHtmlOperationBeans=htmlProjectService.selectLibrary(projectId);
		for (DbInfo dbInfo : lIbraryHtmlOperationBeans) {		
			dbInfo.setJdbcUrl(dbInfo.getJdbcUrl().substring(33));
		}
		return new Result(true, "查询成功", lIbraryHtmlOperationBeans);
	}
	
	
	 //查询表
	@ResponseBody
	@RequestMapping("selectionTable")
	public Result 	selectionTable(HttpServletRequest request, HttpServletResponse response)throws SQLException {	
		String selectionLibrary = request.getParameter("selectionLibrary");
		List<HtmlOperation> lIbraryHtmlOperation=htmlProjectService.tableHtml(selectionLibrary);
		return new Result(true, "查询成功", lIbraryHtmlOperation);
	}
	
	
	
	 //查询字段
	@ResponseBody
	@RequestMapping("selectionField")
	public Result 	selectionField(HttpServletRequest request, HttpServletResponse response)throws SQLException {	
		String selectionTable = request.getParameter("selectionTable");
		List<HtmlOperation> lIbraryHtmlOper=htmlProjectService.fieldHtml(selectionTable);
		return new Result(true, "查询成功", lIbraryHtmlOper);
	}
	
}
